package org.example.utils.user;

import org.example.dao.dao.user.UUserDao;
import org.example.dao.dto.user.UserDto;
import org.example.dao.entity.user.AuPermission;
import org.example.dao.entity.user.AuRole;
import org.example.exception.ParamLossErrorException;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.redis.RedisConstants;
import org.example.skytool.redis.RedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static org.example.skytool.redis.RedisConstants.*;

/**
 * @author 22449
 */
@Component
public class RedisUserService {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private ContextUtil contextUtil;

    public String getCommentNoReadListHeader(String id) {
        String header = USER_MOUDLE + id + USER_COMMENT_CONTROLLER +
                USER_COMMENT_LIST_KIND + USER_COMMENT_LIST_NOREAD_KIND;
        return header;
    }

    public String getCommentReadListHeader(String id) {
        String header = USER_MOUDLE + id + USER_COMMENT_CONTROLLER +
                USER_COMMENT_LIST_KIND + USER_COMMENT_LIST_READ_KIND;
        return header;
    }

    public String getReplyNoReadListHeader(String id) {
        String header = USER_MOUDLE + id + USER_REPLY_CONTROLLER +
                USER_REPLY_LIST_KIND + USER_REPLY_LIST_NOREAD_KIND;
        return header;
    }

    public String getReplyReadListHeader(String id) {
        String header = USER_MOUDLE + id + USER_REPLY_CONTROLLER +
                USER_REPLY_LIST_KIND + USER_REPLY_LIST_READ_KIND;
        return header;
    }


    private String getCacheTokenHeader(String userId) {
        String header = USER_MOUDLE + userId + USER_OAA_CONTROLLER + USER_OAA_JWT_KIND;
        return header;
    }

    private String getCachePermissionHeader(String userId) {
        String header = USER_MOUDLE + userId + USER_OAA_CONTROLLER + USER_OAA_PERMISSION_KIND;
        return header;
    }

    private String getCacheRoleHeader(String userId) {
        String header = USER_MOUDLE + userId + USER_OAA_CONTROLLER + USER_OAA_ROLE_KIND;
        return header;
    }

    private String getCookieHeader(String userId) {
        String header = USER_MOUDLE + userId + USER_JW_CONTROLLER + USER_JW_COOKIE_KIND;
        return header;
    }

    private String getUserDtoKey(String userId) {
        String key = USER_MOUDLE + userId + USER_DATA_CONTROLLER + USER_DATA_DTO_CONTROLLER;
        return key;
    }

    /**
     * 存入token
     */
    public void setCacheToken(String userId, String token) {
        redisUtils.stringSet(getCacheTokenHeader(userId), token);
    }

    /**
     * 获取token
     */
    public String getCacheToken(String userId) {
        return redisUtils.stringGet(getCacheTokenHeader(userId));
    }

    /**
     * 存入cookie
     */
    public void setCacheCookie(String userId, String cookie) {
        redisUtils.stringSet(getCookieHeader(userId), cookie, 30L, TimeUnit.MINUTES);
    }

    /**
     * 获取cookie
     */
    public String getCacheCookie(String userId) {
        return redisUtils.stringGet(getCookieHeader(userId));
    }

    /**
     * 存入UserDto
     */
    public void setUserDto(UserDto userDto) {
        String userId = userDto.getUser().getId().toString();
        redisUtils.objectSet(getUserDtoKey(userId), userDto, 30L, TimeUnit.MINUTES);
    }

    @Resource
    private UUserDao uUserDao;

    /**
     * 获取UserDto
     */
    public UserDto getUserDto(String userId) {
        UserDto userDto = redisUtils.objectGet(getUserDtoKey(userId), UserDto.class);
        if(userDto==null){
            userDto = uUserDao.selectUserDto(Long.valueOf(userId));
            setUserDto(userDto);
        }
        return userDto;
    }

    public void delUserDto(String userId){
        redisUtils.del(getUserDtoKey(userId));
    }

    public void setCachePermission(UserDto userDto) {
        String userId = userDto.getUser().getId().toString();
        Set<String> role = new HashSet<>();
        Set<String> permission = new HashSet<>();
        for(AuRole role1 : userDto.getRoleList()){
            role.add(role1.getName());
        }
        for(AuPermission permission1 : userDto.getPermissionList()){
            permission.add(permission1.getName());
        }
        setCachePermission(userId,role,permission);
    }

    /**
     * 存入权限信息和角色信息
     */
    public void setCachePermission(String userId, Set<String> role, Set<String> permission) {
        if (role.size() != 0) {
            redisUtils.setAdd(getCacheRoleHeader(userId), role);
        }
        if (permission.size() != 0) {
            redisUtils.setAdd(getCachePermissionHeader(userId), permission);
        }
    }

    /**
     * 判断是否包含权限信息
     */
    public Boolean judgePermission(String userId, List<String> list) {
        String key = getCachePermissionHeader(userId);
        for (String permission : list) {
            if (!redisUtils.setIsMember(key, permission)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断是否包含角色信息
     */
    public Boolean judgeRole(String userId, List<String> list) {
        String key = getCacheRoleHeader(userId);
        for (String role : list) {
            if (!redisUtils.setIsMember(key, role)) {
                return false;
            }
        }
        return true;
    }

    public void addUserReplyNoReadList(String userId, String replyId) {
        redisUtils.listLeftAdd(getReplyNoReadListHeader(userId), replyId);
    }

    public void addUserReplyReadList(String userId, String replyId) {
        redisUtils.listLeftAdd(getReplyReadListHeader(userId), replyId);
    }

    public void addUserCommentNoReadList(String userId, String commentId) {
        redisUtils.listLeftAdd(getCommentNoReadListHeader(userId), commentId);
    }

    public void addUserCommentReadList(String userId, String commentId) {
        redisUtils.listLeftAdd(getCommentReadListHeader(userId), commentId);
    }


    public List<String> getReplyByToMine(Integer isread, Long limit, Long start) {
        List<String> list = null;
        String userId = contextUtil.getId().toString();
        if (1 == isread) {
            list = redisUtils.getListKeyNoDelete(getReplyReadListHeader(userId), start, limit);
        } else if (0 == isread) {
            list = redisUtils.getListKey(getReplyNoReadListHeader(userId), start, limit);
        } else {
            throw new ParamLossErrorException("已读传递1，未读传递0，您传递的为：" + isread);
        }
        if (list.size() == 0) {
            return null;
        }
        return list;
    }

    public List<String> getCommentByToMine(Integer isread, Long limit, Long start) {
        List<String> list = null;
        String userId = contextUtil.getId().toString();
        if (1 == isread) {
            list = redisUtils.getListKeyNoDelete(getCommentReadListHeader(userId), start, limit);
        } else if (0 == isread) {
            list = redisUtils.getListKey(getCommentNoReadListHeader(userId), start, limit);
        } else {
            throw new ParamLossErrorException("已读传递1，未读传递0，您传递的为：" + isread);
        }
        if (list.size() == 0) {
            return null;
        }
        return list;
    }


}
